#!/bin/bash
# init omv repos

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions
. /usr/share/openmediavault/sysinfo/functions

declare -i i=0

export LANG=C

((i++))
echo -e "${i}. Init apt repos ..."

omv-env set OMV_APT_REPOSITORY_URL "https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/public"
omv-env set OMV_APT_ALT_REPOSITORY_URL "https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/packages"
omv-env set OMV_APT_KERNEL_BACKPORTS_REPOSITORY_URL "https://mirrors.tuna.tsinghua.edu.cn/debian"
omv-env set OMV_APT_SECURITY_REPOSITORY_URL "https://mirrors.tuna.tsinghua.edu.cn/debian-security"
omv-env set OMV_EXTRAS_APT_REPOSITORY_URL "https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers"
omv-env set OMV_EXTRAS_KEY_URL "https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers/omvextras2026.asc"
omv-env set OMV_DOCKER_APT_REPOSITORY_URL "https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian"
omv-env set OMV_DOCKER_KEY_URL "https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian/gpg"

omv-salt stage run --quiet prepare
omv-salt deploy run --quiet apt

((i++))
echo -e "\n${i}. Cleaning apt cache files..."
/usr/bin/apt-get clean

((i++))
echo -e "\n${i}. Clearing available dpkg packages..."
/usr/bin/dpkg --clear-avail

((i++))
echo -e "\n${i}. Removing apt lists..."
rm -rfv /var/lib/apt/lists/*

((i++))
echo -e "\n${i}. Removing OpenMediaVault cache packages..."
rm -fv /var/cache/openmediavault/archives/*

((i++))
echo -e "\n${i}. Creating new OpenMediaVault packages file..."
touch /var/cache/openmediavault/archives/Packages

# add repos
arch="$(dpkg --print-architecture)"
dist="$(lsb_release --codename --short)"
omv_codename=$(omv_product_info "versionname")

echo "APT::Get::Update::SourceListWarnings::NonFreeFirmware \"false\";" > /etc/apt/apt.conf.d/no-"${dist,,}"-firmware.conf

repo_url="https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers"
key_url="https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers/omvextras2026.asc"
docker_url="https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian"
docker_key_url="https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian/gpg"

key="/usr/share/keyrings/omvextras.gpg"
docker_key="/usr/share/keyrings/docker.gpg"
list="/etc/apt/sources.list.d/omvextras.list"
docker_list="/etc/apt/sources.list.d/omvdocker.list"

((i++))
echo -e "\n${i}. Remove old omv-extras apt list files..."
grep -l 'openmediavault-plugin-developers' /etc/apt/sources.list.d/* | sudo xargs -r rm -fv
rm -fv /etc/apt/keyrings/docker.gpg

((i++))
echo -e "\n${i}. Adding omv-extras repo and key..."
mkdir -pv "$(dirname ${key})"
echo "deb [signed-by=${key} arch=${arch}] ${repo_url} ${omv_codename,,} main" > ${list}
wget --quiet --output-document=- "${key_url}" | gpg --dearmor > "${key}"
chmod 644 ${list} ${key}

# docker repo
grep -l 'docker' /etc/apt/sources.list.d/* | sudo xargs -r rm -fv
((i++))
echo -e "\n${i}. Adding docker repo and key..."
echo "deb [signed-by=${docker_key} arch=${arch}] ${docker_url} ${dist,,} stable" > ${docker_list}
wget --quiet --output-document=- "${docker_key_url}" | gpg --dearmor > "${docker_key}"
chmod 644 ${docker_list} ${docker_key}

((i++))
echo -e "\n${i}. Updating apt..."
/usr/bin/apt-get update

exit 0